package sangpil.presentation
{
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.geom.ColorTransform;
	import flash.utils.clearInterval;
	import flash.utils.setInterval;
	
	import gs.TweenLite;
	import gs.easing.Cubic;
	
	import sangpil.effects.CircleArm;
	import sangpil.events.GeneralEvent;

	[Event(name = 'changed', type = 'sangpil.events.GeneralEvent')]
	public class Dot extends Sprite
	{
		//private variables
		private var radious:Number;
		private var dotColor:uint;
		private var bigTime:Number;
		private var smallTime:Number;
		private var subMenuStr:Array;
		private var subDots:CircleArm;
		private var subGap:Number;
		
		public var orgPosX:Number;
		public var orgPosY:Number;
		
		//children
		private var dot:dotClip;
		
		public function Dot(radious:Number, color:uint)
		{
			super();
			this.radious = radious;
			this.dotColor = color;
			this.subMenuStr = new Array('Close','Check','Develop','Reserve','Color','Delete'); //Temp data. it will be changed.
			
			this.init();
		}
		
		private function init():void
		{
			this.makeDotSub();
			this.makeDot();
			this.addEvent();
		}
		
		private function makeDot():void
		{
			this.dot = new dotClip();
			this.buttonMode = true;
			this.addChild(this.dot);
			this.dot.alpha = 0;
			
			this.makeBig();
		}
		
		private function makeDotSub():void
		{
			this.subGap = -10;
			this.subDots = new CircleArm(this.radious + this.subGap, this.subMenuStr, this.radious / 3);
			this.addChild(this.subDots);
		}
		
		private function makeBig():void
		{
			TweenLite.to(this.dot, this.bigTime, {alpha:1, width:this.radious, height:this.radious, ease:Cubic.easeOut, onComplete:onFinishTween, onCompleteParams:[this.dot, 'default']});
		}
		
		private function showMenu():void
		{
			trace('show menu');
			this.subDots.showArm();
		}
		
		private function hideMenu():void
		{
			trace('hide menu');
			this.subDots.hideArm();
		}
		
		private function addEvent():void
		{
			//mouse Event
			//this.dot.addEventListener(MouseEvent.MOUSE_DOWN, onDown);
			this.dot.addEventListener(MouseEvent.MOUSE_OVER, onOver);
			this.dot.addEventListener(MouseEvent.MOUSE_MOVE, onMove);
			this.dot.addEventListener(MouseEvent.MOUSE_OUT, onOut);
			
			//for subMenu;
			this.subDots.addEventListener(GeneralEvent.CHANGED, subChanged);
			
			//keyboard Event
			this.addEventListener(Event.ADDED_TO_STAGE, keyEvent);
		}
		
		/* submenu event */
		private function subChanged(e:GeneralEvent):void
		{
			trace(e.str);
			this.dispatchEvent(new GeneralEvent(e.str,GeneralEvent.CHANGED));
			switch(e.str)
			{
				case 'Close':
					this.hideMenu();
					this.menuSelected = false;
				break;
				case 'Delete':
					this.removeChild(this.dot);
					this.removeChild(this.subDots);
					this.parent.removeChild(this);
				break;
				case 'Develop':
					this.develop();
				break;
				case 'Check':
					var colTrans:ColorTransform = new ColorTransform();
					colTrans.color = 0xffDE26;
					this.dot.transform.colorTransform = colTrans;
					this.hideMenu();
					this.menuSelected = false;
				break;
				case 'Reserve':
					this.dot.transform.colorTransform = new ColorTransform(1, 1, 1, 0.1);
					this.hideMenu();
					this.menuSelected = false;
				break;
				case 'Color':
					this.dot.transform.colorTransform = new ColorTransform();
					this.hideMenu();
					this.menuSelected = false;
				break;
				default:
				break;
			}
		}
		
		public var developed:Boolean = false;
		private var intervalId:uint;
		/* when select develop button*/
		private function develop():void
		{
			this.dot.transform.colorTransform = new ColorTransform();
			this.dot.gotoAndPlay(2);
			this.menuSelected = false;
			this.developed = true;
			this.hideMenu();
			this.intervalId = setInterval(this.drawCircle, 100);
		}


		
		private var countDev:uint = 0;
		private var devShapeArr:Array = [];
		private var devShapePosArr:Array = [];
		private function drawCircle():void
		{
			var devShape:Shape = new Shape();
			this.devShapeArr.push(devShape);
			if(this.countDev < Math.round(Math.random()*50))
			{	
				devShape.graphics.beginFill(this.dotColor+20*this.countDev);
				devShape.graphics.drawCircle(0, 0, this.radious/2);
				devShape.graphics.endFill();
				
				this.dot.addChild(devShape);
				devShape.z = this.countDev * -5;
				this.devShapePosArr.push(devShape.z);
				
				this.countDev+=1;
			}else{
				devShape.graphics.beginFill(0xE2F0D6);
				devShape.graphics.drawCircle(0, 0, this.radious/2);
				devShape.graphics.endFill();
				
				this.dot.addChild(devShape);
				devShape.z = this.countDev * -5;
				this.devShapePosArr.push(devShape.z);
				
				clearInterval(this.intervalId);
				
				this.zipDevShape();
			}
		}

		public function zipDevShape():void
		{
			if(this.developed)
			{
				for(var i:uint = 0; i < this.devShapeArr.length; i++)
				{
					TweenLite.to(this.devShapeArr[i], 1, {ease:Cubic.easeInOut, z:0});
				}
			}
		}
		
		public function unZipDevShape():void
		{
			trace('unZip');
			if(this.developed)
			{
				for(var i:uint = 0; i < this.devShapeArr.length; i++)
				{
					TweenLite.to(this.devShapeArr[i], 0.5, {ease:Cubic.easeInOut, z:this.devShapePosArr[i]});
				}
			}
		}
		
		/* mouseEvent */		
		private function onDown(e:MouseEvent):void
		{
			
		}
		
		private function onOver(e:MouseEvent):void
		{
			if(this.overSecondTime && this.menuSelected == false)
			{
				if(!this.developed)
				{
					this.showMenu();
					this.menuSelected = true;
				}else{
					this.unZipDevShape();
				}
			}
		}
		
		private var overSecondTime:Boolean = false;
		private var menuSelected:Boolean = false;
		private function onOut(e:MouseEvent):void
		{
			if(this.overSecondTime && this.menuSelected == false)
			{
				if(!this.developed)
				{
					this.hideMenu();
				}else{
					this.zipDevShape();
				}
			}
			this.overSecondTime = true;
		}
		
		private function onMove(e:MouseEvent):void
		{
			
		}
		
		/* keyboardEvent */
		private function keyEvent(e:Event):void
		{
			this.addEventListener(KeyboardEvent.KEY_DOWN, keyDown);
		}
		
		private function keyDown(e:KeyboardEvent):void
		{
			
		}
		
		private function onFinishTween(mc:Sprite, mode:String):void
		{
			switch(mode)
			{
				case 'default':
				break;
				default :
				trace('it has not mode');
			}
		}
		
	}
}